local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local menuAudio = audio.loadSound("menuSong.wav")
local audioChannel

----------------------------------------------------------------------------------
-- 
--      NOTE:
--      
--      Code outside of listener functions (below) will only be executed once,
--      unless storyboard.removeScene() is called.
-- 
---------------------------------------------------------------------------------


-- local forward references should go here --


---------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------

-- Called when the scene's view does not exist:
function scene:createScene( event )
        local group = self.view
		
        -----------------------------------------------------------------------------

        --      CREATE display objects and add them to 'group' here.
        --      Example use-case: Restore 'group' from previously saved state.

        -----------------------------------------------------------------------------
		
		print(display.contentWidth)
		background = display.newImageRect("menubg.png",BASE_SCREEN_WIDTH,BASE_SCREEN_HEIGHT)
		--background:setFillColor(128,179,255)
		background.x = display.contentWidth / 2
		background.y = display.contentHeight / 2
		
		
		gameLogo = display.newImageRect("mainLogo.png",223,156)
		gameLogo.x = display.contentWidth / 2
		gameLogo.y = 90
		
		buttonUpGameCenter = display.newImageRect("gamecntbuttonUp.png",78,74)
		buttonDownGameCenter = display.newImageRect("gamecntbuttonDown.png",75,72)
		buttonUpGameCenter.isVisible = false
		
		buttonUpCredits = display.newImageRect("menuCreditsBntUp.png",89,99)
		buttonDownCredits = display.newImageRect("menuCreditsBntDonw.png",84,88)
		
		buttonUpFacebook = display.newImageRect("facebuttonUp.png",77,73)
		buttonDownFacebook = display.newImageRect("facebuttonDown.png",75,72)
		
		buttonUpNewGame = display.newImageRect("newButtonUp.png",277,69)
		buttonDownNewGame = display.newImageRect("newButtonDown.png",268,59)
		
		buttonUpCotinue = display.newImageRect("cntbuttonUp.png",274,66)
		buttonDownContinue = display.newImageRect("cntbuttonDown.png",268,59)
		
		buttonDownFacebook.isVisible = false
		buttonDownCredits.isVisible = false
		
		buttonUpCredits.x = display.contentWidth/2
		buttonUpCredits.y = display.contentHeight - 55
		
		buttonDownCredits.x = display.contentWidth/2
		buttonDownCredits.y = display.contentHeight - 55
		
		buttonDownFacebook.x = display.contentWidth - 50
		buttonDownFacebook.y = display.contentHeight - 50
		
		buttonUpFacebook.x = display.contentWidth - 50
		buttonUpFacebook.y = display.contentHeight - 50
				
		buttonDownGameCenter.isVisible = false

		buttonDownGameCenter.x = 50
		buttonDownGameCenter.y = display.contentHeight - 50
		
		buttonUpGameCenter.x = 50
		buttonUpGameCenter.y = display.contentHeight - 50
		
		buttonUpNewGame.y = display.contentHeight/2
		buttonUpNewGame.x = display.contentWidth/2
		
		buttonDownNewGame.y = display.contentHeight/2
		buttonDownNewGame.x = display.contentWidth/2
		
		buttonDownNewGame.isVisible = false
		
		buttonUpCotinue.y = display.contentHeight/2 + 100
		buttonUpCotinue.x = display.contentWidth/2
		
		buttonDownContinue.y = display.contentHeight/2 + 100
		buttonDownContinue.x = display.contentWidth/2
		
		buttonDownContinue.isVisible = false
		
		group:insert(background)
		
		group:insert(buttonUpCredits)
		group:insert(buttonDownCredits)
		
		group:insert(buttonUpNewGame)
		group:insert(buttonDownNewGame)
				
		group:insert(buttonUpCotinue)
		group:insert(buttonDownContinue)
		
		group:insert(buttonUpGameCenter)
		group:insert(buttonDownGameCenter)
		
		
		group:insert(buttonUpFacebook)
		group:insert(buttonDownFacebook)
		
		
		group:insert(gameLogo)
		
				
		--[[
			Credits listener		
		]]--
				
		local function newCreditsListener(event) 			
			local function creditsScreen()
				print("Goto Credits")
				buttonDownCredits.isVisible = false
				buttonUpCredits.isVisible = true
				storyboard.gotoScene("Credits",{time=500,effect="slideLeft" })
			end			
			if (event.phase == "began") then
				buttonDownCredits.isVisible = true
				buttonUpCredits.isVisible = false
				timer.performWithDelay(100,creditsScreen,1)
								
			elseif (event.phase == "ended") then
				--buttonDownNewGame.isVisible = false
				--buttonUpNewGame.isVisible = true
			end						
			return true
		end 
		buttonUpCredits:addEventListener( "touch", newCreditsListener )
				
				
				
		--[[
			New game listener		
		]]--
				
		local function newGameListener(event) 			
			local function newGame()
				print("Goto Game Play")
				buttonDownNewGame.isVisible = false
				buttonUpNewGame.isVisible = true
				local persistence = require "Persistence" 
				global_levelNumber = 1
				global_totalHighScore = 0
				persistence.resetData()
				
				storyboard.gotoScene("GamePlay",{time=500,effect="slideLeft" })
			end			
			if (event.phase == "began") then
				buttonDownNewGame.isVisible = true
				buttonUpNewGame.isVisible = false
				timer.performWithDelay(100,newGame,1)
								
			elseif (event.phase == "ended") then
				--buttonDownNewGame.isVisible = false
				--buttonUpNewGame.isVisible = true
			end						
			return true
		end 
		buttonUpNewGame:addEventListener( "touch", newGameListener )
		
		--[[
			Continue game listener		
		]]--
		
		local function continueGameListener(event) 			
			local function continueGame()
				print("Continue Game")
				buttonDownContinue.isVisible = false
				buttonUpCotinue.isVisible = true
				storyboard.gotoScene("GamePlay",{time=500,effect="slideLeft" })
			end			
			if (event.phase == "began") then
				buttonDownContinue.isVisible = true
				buttonUpCotinue.isVisible = false
				timer.performWithDelay(100,continueGame,1)		
				
			else
				--buttonDownContinue.isVisible = false
				--buttonUpCotinue.isVisible = true
				
			end						
			return true
		end 
		buttonUpCotinue:addEventListener( "touch", continueGameListener )
		
		--[[
			Game Center button
		]]--
		
		local function gameCenterListener(event) 			
			local function submitScore()
				buttonDownGameCenter.isVisible = false
				buttonUpGameCenter.isVisible = true
				gameCenter = require "GameCenter"
				gameCenter.showLeaderBoard()
				
				--start game center here
				--storyboard.gotoScene("GamePlay",{time=500,effect="slideLeft" })
			end			
			if (event.phase == "began") then
				buttonDownGameCenter.isVisible = true
				buttonUpGameCenter.isVisible = false
				timer.performWithDelay(100,submitScore,1)
			else
				--buttonDownGameCenter.isVisible = false
				--buttonUpGameCenter.isVisible = true
				
			end						
			return true
		end 
		buttonUpGameCenter:addEventListener( "touch", gameCenterListener )
		
		--[[
			Facebook button
		]]--
		
		local function facebookListener(event) 			
			local function submitScoreFacebook()
				print("Submit Score Facebook")
				
				buttonDownFacebook.isVisible = false
				buttonUpFacebook.isVisible = true
				
				local gameFacebook = require "GameFacebook"
				local persistence = require "Persistence"
				
				local highscore = 0
				
				for contLevel=1,global_levelNumber do
					highscore = highscore + tonumber(persistence.loadValue(HIGH_SCORE_SAVE_FILE..contLevel))
				end
				gameFacebook.postHighScore(highscore)	
			end			
			if (event.phase == "began") then
				print("Event began")
				buttonDownFacebook.isVisible = true
				buttonUpFacebook.isVisible = false
				timer.performWithDelay(100,submitScoreFacebook,1)
			elseif (event.phase == "ended") then
				--print("Event ended")
				--buttonDownFacebook.isVisible = false
				--buttonUpFacebook.isVisible = true
				
			end						
			return true
		end 
		buttonUpFacebook:addEventListener( "touch", facebookListener )
end


-- Called BEFORE scene has moved onscreen:
function scene:willEnterScene( event )
        local group = self.view

        -----------------------------------------------------------------------------

        --      This event requires build 2012.782 or later.

        -----------------------------------------------------------------------------

end


-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
        local group = self.view
		audioChannel = audio.play(menuAudio,{loops = -1})
        -----------------------------------------------------------------------------

        --      INSERT code here (e.g. start timers, load audio, start listeners, etc.)

        -----------------------------------------------------------------------------

end


-- Called when scene is about to move offscreen:
function scene:exitScene( event )
        local group = self.view
		audio.stop(audioChannel)
        -----------------------------------------------------------------------------

        --      INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)

        -----------------------------------------------------------------------------

end


-- Called AFTER scene has finished moving offscreen:
function scene:didExitScene( event )
        local group = self.view

        -----------------------------------------------------------------------------

        --      This event requires build 2012.782 or later.

        -----------------------------------------------------------------------------

end


-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
        local group = self.view

        -----------------------------------------------------------------------------

        --      INSERT code here (e.g. remove listeners, widgets, save state, etc.)

        -----------------------------------------------------------------------------

end


-- Called if/when overlay scene is displayed via storyboard.showOverlay()
function scene:overlayBegan( event )
        local group = self.view
        local overlay_name = event.sceneName  -- name of the overlay scene

        -----------------------------------------------------------------------------

        --      This event requires build 2012.797 or later.

        -----------------------------------------------------------------------------

end


-- Called if/when overlay scene is hidden/removed via storyboard.hideOverlay()
function scene:overlayEnded( event )
        local group = self.view
        local overlay_name = event.sceneName  -- name of the overlay scene

        -----------------------------------------------------------------------------

        --      This event requires build 2012.797 or later.

        -----------------------------------------------------------------------------

end



---------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )

-- "willEnterScene" event is dispatched before scene transition begins
scene:addEventListener( "willEnterScene", scene )

-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched before next scene's transition begins
scene:addEventListener( "exitScene", scene )

-- "didExitScene" event is dispatched after scene has finished transitioning out
scene:addEventListener( "didExitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )

-- "overlayBegan" event is dispatched when an overlay scene is shown
scene:addEventListener( "overlayBegan", scene )

-- "overlayEnded" event is dispatched when an overlay scene is hidden/removed
scene:addEventListener( "overlayEnded", scene )

---------------------------------------------------------------------------------

return scene